package com.araaya.eslimsmobile;

import java.io.File;

import com.araaya.eslimsmobile.R;
import com.araaya.eslimsmobile.util.ElandConstant;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Window;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class TestWebHome extends Activity {
	/** Called when the activity is first created. */

	WebView webView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		

		// initialization of webview

		//webView = (WebView) findViewById(R.layout.web_home);
		setContentView(R.layout.web_home);
		webView = (WebView) findViewById(R.id.webview);
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);

		// Loading HTML in WebView
		// webView.loadUrl("file:///android_res/raw/testhtml.html");
		//webView.loadUrl(ElandConstant.MAIN_SITE_URL);

		// Setting WebChromeClient to webView
		//webView.setWebChromeClient(new MyJavaScriptChromeClient());
		webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
		webView.loadUrl(ElandConstant.MAIN_SITE_URL);
	}
	
	//@Override
	/*protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
	    
		Uri selectedImage;
		android.provider.ContactsContract.Contacts.Photo photo;
		if (resultCode != RESULT_OK) {
	    //** fixed code **//*
	            mUploadMessage.onReceiveValue(null);
	    //** fixed code **//*
	            return;
	    }
	    if (mUploadMessage==null) {
	        Log.d("androidruntime","no mUploadMessage");
	        return;
	    }
	    if (requestCode == FILECHOOSER_RESULTCODE) {
	         selectedImage= intent == null || resultCode != RESULT_OK ? null : intent.getData();
	        Log.d("androidruntime","url: "+selectedImage.toString());

	    }else if (requestCode == CAMERAREQUEST_RESULTCODE) { 
	        if(mCapturedImageURI==null){
	            Log.d("androidruntime","no mCapturedImageURI");
	            return;
	        }
	      //** fixed code **//*
	        getContentResolver().notifyChange(mCapturedImageURI, null);
	        ContentResolver cr = getContentResolver();
	        Uri uriContent= Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), photo.getAbsolutePath(), null, null));
	        photo = null;
	      //** fixed code **//*
	    }
	    mUploadMessage.onReceiveValue(selectedImage);
	    mUploadMessage = null;
	}


	    private static final int FILECHOOSER_RESULTCODE   = 2888;
	    private static final int CAMERAREQUEST_RESULTCODE = 1888;
	    private ValueCallback<Uri> mUploadMessage;
	    private Uri mCapturedImageURI = null;

	    protected class AwesomeWebChromeClient extends WebChromeClient{
	        // Per Android 3.0+
	        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){  
	           *//**updated, out of the IF **//*
	                            mUploadMessage = uploadMsg;
	           *//**updated, out of the IF **//*
	            if(boolFileChooser){ //Take picture from filechooser

	                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
	                i.addCategory(Intent.CATEGORY_OPENABLE);  
	                i.setType("image/*");  
	                TestWebHome.this.startActivityForResult( Intent.createChooser( i, "Escoger Archivo" ), TestWebHome.FILECHOOSER_RESULTCODE );  

	            } else { //Take photo and upload picture
	                Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
	                File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
	                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
	                        Uri.fromFile(photo));
	                mCapturedImageURI = Uri.fromFile(photo);
	                startActivityForResult(cameraIntent, TestWebHome.CAMERA_REQUEST);
	            }
	        }
	        // Per Android < 3.0
	        public void openFileChooser(ValueCallback<Uri> uploadMsg){
	            openFileChooser(uploadMsg, "");
	        }
	    }*/

	private class MyJavaScriptChromeClient extends WebChromeClient {
		
		
		@Override
		public boolean onJsAlert(WebView view, String url, final String message,
				final JsResult result) {
			// handle Alert event, here we are showing AlertDialog
			new AlertDialog.Builder(TestWebHome.this)
					.setTitle("JavaScript Alert !")
					.setMessage(message)
					.setPositiveButton(android.R.string.ok,
							new AlertDialog.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									
									
									result.confirm();
								}
							}).setCancelable(false).create().show();
			return true;

		}
		
	}
}

